#include <asm/sbidef.h>
#include <asm/sbiasm.h>

// #define BOOT_LOADER_SIG_OFFSET 0x1fe
// #define OS_SIZE_LOC (BOOT_LOADER_SIG_OFFSET - 2)
// #define OS_SIZE_LOC 0x1fc

.equ os_size_loc, 0x502001fc

// 1. kernel address (move kernel to here ~)
.equ kernel, 0x50201000

// 2. kernel main address (jmp here to start kernel main!)
.equ kernel_main, 0x50201000
.data
msg: .string "It's a bootloader!\n\r"

.text
.global main

main:
	// fence on all memory and I/O
	fence
	// print a \n to avoid system output error...
	li a0, 10
	SBI_CALL SBI_CONSOLE_PUTCHAR

	// 1) task1 call BIOS print string "It's bootblock!"
	la a0, msg
	SBI_CALL SBI_CONSOLE_PUTSTR

	// 2) task2 call BIOS read kernel in SD card and jump to kernel start
	//lui a0,%hi(0x50500000)
	//addi a0,a0,%lo(0x50500000)
	lui s0,%hi(os_size_loc)
	addi s0,s0,%lo(os_size_loc)
	//lh a1,0(s0)
	//li a2,0
	//SBI_CALL SBI_SD_READ
	//lui a0,%hi(0x50500040)
	//addi a0,a0,%lo(0x50500040)
	//jr a0
	//fence.i
	lui a0,%hi(0x50201000)
	addi a0,a0,%lo(0x50201000)
	//lh a1,0(s0)
	li a1,40
	li a2,1
	SBI_CALL SBI_SD_READ
	lui a0,%hi(0x50201000)
	addi  a0,a0,%lo(0x50201000)
	jr  a0

// while(1) --> stop here
stop:
	j stop
	nop



